﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace MiniFox.Data.Mapping
{
    /// <summary>
    /// 
    /// </summary>
    public class ColumnMappingCollection : List<ColumnMapping>, IColumnMappingCollection
    {
        ITableMapping _tableMapping;
        internal ColumnMappingCollection(ITableMapping tableMapping)
        {
            _tableMapping = tableMapping;
        }
        #region IColumnMappingCollection 成员
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourceColumnName"></param>
        /// <param name="dataSetColumnName"></param>
        /// <returns></returns>
        public IColumnMapping Add(string sourceColumnName, string dataSetColumnName)
        {
            ColumnMapping cm = new ColumnMapping(sourceColumnName, dataSetColumnName, this._tableMapping);
            this.Add(cm);
            return cm;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourceColumnName"></param>
        /// <returns></returns>
        public bool Contains(string sourceColumnName)
        {
            Predicate<ColumnMapping> predicate = new Predicate<ColumnMapping>(
                delegate(ColumnMapping mapping) { return mapping.SourceColumn == sourceColumnName; }
                );
            return this.Exists(predicate);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataSetColumnName"></param>
        /// <returns></returns>
        public IColumnMapping GetByDataSetColumn(string dataSetColumnName)
        {
            Predicate<ColumnMapping> predicate = new Predicate<ColumnMapping>(
                delegate(ColumnMapping mapping) { return mapping.DataSetColumn == dataSetColumnName; }
                );
            return this.Find(predicate);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourceColumnName"></param>
        /// <returns></returns>
        public ColumnMapping GetBySourceColumn(string sourceColumnName)
        {
            Predicate<ColumnMapping> predicate = new Predicate<ColumnMapping>(
                delegate(ColumnMapping mapping) { return mapping.SourceColumn == sourceColumnName; }
                );
            return this.Find(predicate);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourceColumnName"></param>
        /// <returns></returns>
        public int IndexOf(string sourceColumnName)
        {
            return this.IndexOf(this.GetBySourceColumn(sourceColumnName));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourceColumnName"></param>
        public void RemoveAt(string sourceColumnName)
        {
            this.RemoveAt(this.IndexOf(sourceColumnName));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataSetColumnName"></param>
        /// <returns></returns>
        public object this[string dataSetColumnName]
        {
            get
            {
                return GetByDataSetColumn(dataSetColumnName);
            }
            set
            {
                ColumnMapping val = (ColumnMapping)value;
                Predicate<ColumnMapping> predicate = new Predicate<ColumnMapping>(
                    delegate(ColumnMapping mapping) { return mapping.DataSetColumn == dataSetColumnName; }
                    );
                int i = this.FindIndex(predicate);
                base[i] = val;
            }
        }

        #endregion
    }
}
